//
//  TTAddMemberViewController.m
//  TeamTiger
//
//  Created by Dale on 17/2/3.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

#import "TTAddMemberViewController.h"
#import "WXApiManager.h"
#import "WXApiRequestHandler.h"
#import "Constant.h"
#import "TTAddMemberCell.h"
#import "HYBHelperKit.h"

@interface TTAddMemberViewController () <UITableViewDataSource, UITableViewDelegate, WXApiManagerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *membersArray;//与我有关系的成员数组
@property (strong, nonatomic) NSMutableArray *selectMembers;
@property (nonatomic,strong) UIButton *rightBtn;

@end

@implementation TTAddMemberViewController

#pragma mark - getter 
- (NSMutableArray *)membersArray {
    if (_membersArray == nil) {
        _membersArray = [NSMutableArray array];
    }
    return _membersArray;
}

- (NSMutableArray *)selectMembers {
    if (_selectMembers == nil) {
        _selectMembers = [NSMutableArray array];
    }
    return _selectMembers;
}

- (NSMutableArray *)members {
    if (_members == nil) {
        _members = [NSMutableArray array];
    }
    return _members;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"添加成员";
    [super configureLeftNavigationBarItem];
    [super configureRightNavigationBarItemWithTitle:@"确定"];
    [self userRelation];
    [WXApiManager sharedManager].delegate = self;
    self.tableView.rowHeight = 70;
    [Common removeExtraCellLines:self.tableView];
}

- (void)handleLeftBtnAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleRightBtnAction {
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.membersArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JFUser *member = self.membersArray[indexPath.row];
    
    static NSString *ID = @"TTAddMemberCell";
    TTAddMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        if ([Common isEmptyString:member.user_id]) {
            cell = LoadFromNib(@"TTAddMemberCell2");
        } else {
            cell = LoadFromNib(@"TTAddMemberCell");
        }
    }
    cell.user = member;
    [cell setSelectBtnBlock:^(TTAddMemberCellType type){
        if (type == TTAddMemberCellTypeSelectMember) {
            
        } else {
             [self toWeChatAddMember];
        }
        
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JFUser *member = self.membersArray[indexPath.row];
    member.isSelect = !member.isSelect;
    [self.tableView reloadData];
}

- (void)toWeChatAddMember {
    NSLog(@"跳转微信，增加人员");
    UIImage *thumbImage = [UIImage imageNamed:@"AppIcon"];
    
    //          方式一:
    //                NSData *data = [@"cehuabar" dataUsingEncoding:NSUTF8StringEncoding];
    //                [WXApiRequestHandler sendAppContentData:data
    //                                                ExtInfo:kAppContentExInfo //拼接参数
    //                                                 ExtURL:kAppContnetExURL //可以填app的下载地址
    //                                                  Title:kAPPContentTitle
    //                                            Description:kAPPContentDescription
    //                                             MessageExt:kAppMessageExt
    //                                          MessageAction:kAppMessageAction
    //                                             ThumbImage:thumbImage
    //                                                InScene:WXSceneSession];
    //          方式二:
    
    NSString *composeURL = [NSString stringWithFormat:@"%@?%@",kLinkURL, @""];
    composeURL = [composeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [WXApiRequestHandler sendLinkURL:composeURL
                             TagName:kLinkTagName
                               Title:kLinkTitle
                         Description:kLinkDescription
                          ThumbImage:thumbImage
                             InScene:WXSceneSession];
}

#pragma mark - 获取与当前用户存在项目关系的用户
- (void)userRelation{
    UserRelatedApi *api = [[UserRelatedApi alloc] init];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"UserRelatedApi:%@", request.responseObject);
        NSData *data = [request.responseObject[@"response"] dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *dataArr = dic[DATA];
        if ([dic[CODE] intValue] == 200) {
            if (![Common isEmptyArr:dataArr]) {
                for (NSDictionary *membersDic in dataArr) {
                    JFUser *member = [[JFUser alloc] initWithUser:membersDic];
                    [self.membersArray addObject:member];
                }
                for (NSDictionary *membersDic in dataArr) {
                    JFUser *member = [[JFUser alloc] initWithUser:membersDic];
                    [self.membersArray addObject:member];
                }
                
//                JFUser *user = [[JFUser alloc] init];
//                user.user_name = @"让微信朋友通过连接加入项目";
//                [self.membersArray addObject:user];
            } else {
//                JFUser *user = [[JFUser alloc] init];
//                user.user_name = @"让微信朋友通过连接加入项目";
//                [self.membersArray addObject:user];
                [super showText:@"暂无相关人员" afterSeconds:1.0];
            }
            [self.tableView reloadData];
        } else {
            [super showText:dic[MESSAGE] afterSeconds:1.0];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"UserRelationApi:%@", request.error.localizedDescription);
        [super showText:request.error.localizedDescription afterSeconds:1.0];
    }];
    }


#pragma -mark WXApiManagerDelegate
- (void)managerDidRecvGetMessageReq:(GetMessageFromWXReq *)request {
    
}

- (void)managerDidRecvShowMessageReq:(ShowMessageFromWXReq *)request {
    //微信回传消息
    [UIAlertView hyb_showWithTitle:@"提示" message:[request.message.mediaObject extInfo] buttonTitles:@[@"确定"] block:^(UIAlertView *alertView, NSUInteger buttonIndex) {}];
}

- (void)managerDidRecvLaunchFromWXReq:(LaunchFromWXReq *)request {
    
}

- (void)managerDidRecvMessageResponse:(SendMessageToWXResp *)response {
    //    返回应用时，收到消息回调
    NSLog(@"ddddddd");
}

- (void)managerDidRecvAuthResponse:(SendAuthResp *)response {
    
}

@end
